package cn.lxh.auto_configuration;

import cn.lxh.properties.WxMessageProperties;
import cn.lxh.properties.WxMinProperties;
import cn.lxh.template.WeChatMessageTemplate;
import cn.lxh.template.WeChatTemplate;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

/**
 * @author lxh Create Time: 2023/12/23 10:55
 * 微信自动配置类
 */
@Configuration
@EnableConfigurationProperties({WxMinProperties.class, WxMessageProperties.class})
public class WeChatAutoConfiguration {

    @Bean
    public WeChatTemplate weChatTemplate(WxMinProperties wxMinProperties, RestTemplate restTemplate) {
        return new WeChatTemplate(wxMinProperties, restTemplate);
    }

    @Bean
    @ConditionalOnMissingBean // 不存在这个bean才创建, 存在就不创建
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    @Bean
    public WeChatMessageTemplate weChatMessageTemplate(WxMessageProperties wxMessageProperties,
                                                       WeChatTemplate weChatTemplate, RestTemplate restTemplate ) {
        return new WeChatMessageTemplate(wxMessageProperties, weChatTemplate, restTemplate);
    }
}
